<template>
  <div>
    <!-- 搜索 -->
    <!-- 索赔扣款信息查询 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"@resetChange="resetHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
      columnsRef="ClaimDebitsInfoMaintainlgr"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
        <multiuse-button size="small" type="primary"  :click="exportFileBtn.bind()">导出</multiuse-button>
<!--        <multiuse-button size="small" type="primary"  :click="batchExamine.bind()">批量扣款</multiuse-button>-->
      </template>
    </FilterTable>
    <Drawer :visible.sync="visible" :title="dialogTitle" destroyOnClose width="1200px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <xiangqing @close="close" @save="save" :rowData="rowData" :editIds="editIds"></xiangqing>
    </Drawer>
    <Drawer :visible.sync="visibleAdd" :title="'索赔扣款信息新增'" destroyOnClose width="1200px"  :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <add @close="closeAdd" @save="saveAdd" :rowData="rowData"></add>
    </Drawer>
    <Drawer :visible.sync="visibleEdit" :title="'索赔扣款信息维护'" destroyOnClose width="1200px"  :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <edit @close="editclose" @save="editsave" :rowData="rowData" :editIds="editIds"></edit>
    </Drawer>
    <!--  服务中心选择 -->
    <BaseDialog :visible.sync="selectDelearFlag" title="服务中心选择" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="Delearclose" @save="Delearsave" selectionType="multiple" />
    </BaseDialog>
  </div>
</template>
<script>
import xiangqing from './xiangqing.vue';
import add from './addForm.vue';
import edit from './editForm.vue';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { exportExcelFile } from '@/utils/index';
import { getListByPage,getListForExport } from '@/api/claim/factory/ClaimDebitsInfoMaintain';
import { dictionary } from '@/utils/dictMixin';
export default {
  name:'ClaimDebitsInfoMaintain',
  mixins: [dictionary],
  components: {
    xiangqing,
    add,
    edit,
    DealerList

  },
  data() {
    return {
      // 非单个禁用
      // single: true,
      single: false,
      // 非多个禁用
      multiple: true,
      dealerList: [],
      selectDelearFlag: false,
      dealerCompanyCode: [],
      dealerData: [],
      selectDealerData: [],
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: true,
      infoId: null,
      rowData: {},
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {
        xhrAbort: true
      }, //ipa 接口
      fetchapi: getListByPage,
      selectedRows: [], //选中服务中心
      dialogTitle: '详情',
      visibleAdd: false,
      visibleEdit:false,
      visibleAddDealer:false,
      editIds: [],
    };
  },
  created() {
    this.chuqi();
  },
  methods: {
    async chuqi() {
      // const res = await getPartType({ type: 5591 });
      // if (res.resultCode == 200) {
      //   this.topFilterInit.find(x => x.fieldName == 'cbStatus').initialValue = '';
      //   this.topFilterInit.find(x => x.fieldName == 'cbStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'SELECT',
          label: '服务中心',
          render: (p) => {
            return (
              <div>
                <el-select v-model={this.selectDealerData} disabled={true} multiple placeholder="请选择">
                  {this.dealerData.map((item) => (
                    <el-option key={item.dealerCode} label={item.dealerShortname} value={item.dealerCompanyCode} />
                  ))}
                </el-select>
                <multiuse-button style="position: absolute;right: 0;height:100%" icon="el-icon-search" onClick={() => this.selectDelearFlag = true} />
              </div>
            );
          },
          onChange: val => {
            console.log(val)
          }
          // onInput: val => {
          //   // 修改store中 经销商名
          //   setStateParam('searchDealerName', val)
          //   // 重置store中 经销商code
          //   setStateParam('searchDealerCode', null)
          // }
        },
        // { type: 'INPUT', label: '服务中心名称', fieldName: 'dealerName', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '扣款单号', fieldName: 'cbNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '索赔单号', fieldName: 'claimNo', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '扣款单状态', fieldName: 'cbStatus', placeholder: '请选择', initialValue: '', itemList: this.createDictList('5502') }
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val, {
        dealerCode: this.searchDealerCode, dealerCompanyCode: this.selectDealerData.length ? this.selectDealerData : null, xhrAbort: false
      });
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          fixed: 'left',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.chooseOrderNo(props.row)}>
                  查看
                </multiuse-button>
                  <multiuse-button size="mini" type="text" disabled={((props.row.cbStatus == 55021001) )? false : true} onClick={() => this.chooseOrder(props.row)}>
                  编辑
            </multiuse-button>
              </div>
            );
          }
        },
        // 序号、操作、扣款单状态、服务中心代码、服务中心名称、扣款单号、索赔单号、扣款金额、原审核金额、索赔类型、扣款原因、备注、创建人、创建时间、确认人、确认时间；
        { title: '扣款单状态', dataIndex: 'statusStr', sorter: true },
        { title: '服务中心代码', dataIndex: 'dealerCode', sorter: true },
        { title: '服务中心名称', dataIndex: 'dealerName', sorter: true },
        { title: '扣款单号', dataIndex: 'cbNo', sorter: true },
        // { title: '主损件代码', dataIndex: 'partCode', sorter: true },
        // { title: '主损件名称', dataIndex: 'partName', sorter: true },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        { title: '结算单号', dataIndex: 'settleNo', sorter: true },
        // { title: '现结算金额', dataIndex: 'finalSumAmount', sorter: true },
        { title: '扣款金额', dataIndex: 'cbSumAmount', sorter: true,precision: 2, numberFormat: true },
        { title: '审核金额', dataIndex: 'claimSumAmount', sorter: true,precision: 2, numberFormat: true },
        // { title: '申请金额', dataIndex: 'sumAmount', sorter: true },
        { title: '索赔类型', dataIndex: 'claimTypeStr', sorter: true },
        { title: '扣款原因', dataIndex: 'cbReason', sorter: true },
        { title: '备注', dataIndex: 'remarks', sorter: true },
        { title: '创建人', dataIndex: 'createdBy', sorter: true },
        { title: '创建日期', dataIndex: 'createdAt', sorter: true },
        { title: '确认人', dataIndex: 'confirmedBy', sorter: true },
        { title: '确认时间', dataIndex: 'confirmedAt', sorter: true },
        { title: '确认时间', dataIndex: 'cbId', hidden: true }

      ];
    },
    resetHandle(val) {
      this.dealerData = [];
      this.selectDealerData = [];
      this.fetchParams = Object.assign({}, this.fetchParams, val, { dealerCompanyCode: null }, { xhrAbort: false });
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo(val) {
      this.rowData = val;
      this.visible = true;
    },
    chooseOrder(val) {
      this.rowData = val;
      this.visibleEdit = true;
    },
    save(obj) {
      this.topFilterInit.find(x => x.fieldName == 'model').initialValue = obj[0].address;
      this.visible = false;
    },
    // 导出
   async exportFileBtn() {
      // let json = Object.assign({}, { CODE: 'CODE_00000142' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }
      getListForExport(this.fetchParams).then(res => {
        exportExcelFile(res.data, '索赔扣款信息导出');
      });
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    editclose() {
      this.visibleEdit = false;
    },
    editsave() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //服务中心选择页面
    SelectDelear() {
      this.selectDelearFlag = true;
    }, //关闭
    Delearclose() {
      this.selectDelearFlag = false;
    },
    //服务中心选择保存
    Delearsave(obj) {
      console.log('选择数据传递', obj);
      this.dealerData = obj;
      this.selectDealerData = [];
      obj.forEach(item => {
        this.selectDealerData.push(item.dealerCompanyCode);
      });
      this.selectDelearFlag = false;
    },
    chooseDealerNo() {
      this.visibleAddDealer = true;
    },
    batchExamine() {
      if (this.selectedRows.length > 0) {
        this.selectedRows.map(x => {
          this.editIds.push({cbId: x.cbId,status: 0});
        });
        this.visibleEdit = true;
      }else{
        return this.$notify.warning({title: '提示信息', message: '请勾选后再进行扣款！'});
      }

    },
  }
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
